﻿#include <wx/stattext.h>
#include<fstream>
#include<iostream>
#include "Menu.h"
#include"MyFrame.h"
#include"CSFrame.h"
#include"SaveFrame.h"


Menu::Menu(wxPanel* parent)
    : wxPanel(parent, -1, wxPoint(-1, -1), wxSize(100, -1), wxBORDER_SUNKEN)
{
    m_parent = parent;
    m_clear = new wxButton(this, ID_CLEAR, wxT("清除界面"), wxPoint(10, 10));
    m_clear->SetBackgroundColour(wxColour(95, 148, 255)); m_clear->SetForegroundColour(wxColour(255, 255, 255)); m_clear->SetWindowStyleFlag(wxNO_BORDER);
    m_back = new wxButton(this, ID_BACK, wxT("回退一步"),wxPoint(10, 40));
    m_back->SetBackgroundColour(wxColour(95, 148, 255)); m_back->SetForegroundColour(wxColour(255, 255, 255)); m_back->SetWindowStyleFlag(wxNO_BORDER);
    m_setcs = new wxButton(this, ID_SETCS, wxT("设置画笔"),wxPoint(10, 70));
    m_setcs->SetBackgroundColour(wxColour(95, 148, 255)); m_setcs->SetForegroundColour(wxColour(255, 255, 255)); m_setcs->SetWindowStyleFlag(wxNO_BORDER);
    m_select = new wxButton(this, ID_SELECT, wxT("选择"),wxPoint(10, 100));
    m_select->SetBackgroundColour(wxColour(95, 148, 255)); m_select->SetForegroundColour(wxColour(255, 255, 255)); m_select->SetWindowStyleFlag(wxNO_BORDER);
    m_delete = new wxButton(this, ID_DELETE, wxT("删除"),wxPoint(10, 130));
    m_delete->SetBackgroundColour(wxColour(95, 148, 255)); m_delete->SetForegroundColour(wxColour(255, 255, 255)); m_delete->SetWindowStyleFlag(wxNO_BORDER);
    m_copy = new wxButton(this, ID_COPY, wxT("复制"),wxPoint(10, 160));
    m_copy->SetBackgroundColour(wxColour(95, 148, 255)); m_copy->SetForegroundColour(wxColour(255, 255, 255)); m_copy->SetWindowStyleFlag(wxNO_BORDER);

    m_line = new wxBitmapButton(this, ID_LINE, wxBitmap(wxT("直线.png"), wxBITMAP_TYPE_PNG).ConvertToImage().Scale(75, 25), wxPoint(10, 230), wxSize(75, 25));
    m_rect = new wxBitmapButton(this, ID_RECT, wxBitmap(wxT("矩形.png"), wxBITMAP_TYPE_PNG).ConvertToImage().Scale(75, 25), wxPoint(10, 260), wxSize(75, 25));
    m_roud = new wxBitmapButton(this, ID_ROUD, wxBitmap(wxT("圆形.png"), wxBITMAP_TYPE_PNG).ConvertToImage().Scale(75, 25), wxPoint(10, 290), wxSize(75, 25));
    m_arc = new wxBitmapButton(this, ID_ARC, wxBitmap(wxT("弧形.png"), wxBITMAP_TYPE_PNG).ConvertToImage().Scale(75, 25), wxPoint(10, 320), wxSize(75, 25));
    m_point = new wxBitmapButton(this, ID_POINT, wxBitmap(wxT("节点.png"), wxBITMAP_TYPE_PNG).ConvertToImage().Scale(75, 25), wxPoint(10, 350), wxSize(75, 25));

    m_save = new wxButton(this, ID_SAVE, wxT("保存"),wxPoint(10, 580));
    m_save->SetBackgroundColour(wxColour(95, 148, 255)); m_save->SetForegroundColour(wxColour(255, 255, 255)); m_save->SetWindowStyleFlag(wxNO_BORDER);

    Connect(ID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Clear));
    Connect(ID_BACK, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Back));
    Connect(ID_SETCS, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::SetCandS));
    Connect(ID_SELECT, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Select));
    Connect(ID_DELETE, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Delete));
    Connect(ID_COPY, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Copy));

    Connect(ID_LINE, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::UseLine));
    Connect(ID_RECT, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::UseRect));
    Connect(ID_ROUD, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::UseRoud));
    Connect(ID_ARC, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::UseArc));
    Connect(ID_POINT, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::UsePoint));

    Connect(ID_SAVE, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(Menu::Save));

}

void Menu::Clear(wxCommandEvent& WXUNUSED(event)) {
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_br->Clear();

}

void Menu::Back(wxCommandEvent& WXUNUSED(event)){
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_br->Back();
}

void Menu::SetCandS(wxCommandEvent& WXUNUSED(event)) {
    CSFrame* csframe = new CSFrame(wxT("设置画笔"),m_parent);
    wxString imagePath = wxT("自定义元器件.png");
    wxImage appImage(imagePath, wxBITMAP_TYPE_PNG);
    if (!appImage.IsOk()) {
        wxMessageBox(wxT("无法打开文件: ") + imagePath);
    }
    appImage.Rescale(100, 100);
    wxBitmap appBitmap(appImage);
    wxIcon appIcon;
    appIcon.CopyFromBitmap(appBitmap);
    csframe->SetIcon(appIcon);
    csframe->Show(true);


    pencolor = csframe->selectedColor;;
    brushcolor = csframe->selectedBrushColour;

}

void Menu::Select(wxCommandEvent& WXUNUSED(event)) {
    flag = ID_SELECT;
}

void Menu::Delete(wxCommandEvent& WXUNUSED(event)) {
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_br->Delete();
}

void Menu::Copy(wxCommandEvent& WXUNUSED(event)) {
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_br->Copy();
}


void Menu::UseLine(wxCommandEvent& WXUNUSED(event)){
    flag = ID_LINE;
}

void Menu::UseRect(wxCommandEvent& WXUNUSED(event)) {
    flag = ID_RECT;
}

void Menu::UseRoud(wxCommandEvent& WXUNUSED(event)) {
    flag = ID_ROUD;
}

void Menu::UseArc(wxCommandEvent& WXUNUSED(event)) {
    flag = ID_ARC;
}

void Menu::UsePoint(wxCommandEvent& WXUNUSED(event)) {
    flag = ID_POINT;
}

void Menu::Save(wxCommandEvent& WXUNUSED(event)) {
    if (!wxImage::FindHandler(wxBITMAP_TYPE_PNG)) {
        wxImage::AddHandler(new wxPNGHandler);
    }
    SaveFrame* saveframe = new SaveFrame(wxT("保存文件名"), m_parent);
    wxString imagePath = wxT("logo.png");
    wxImage appImage(imagePath, wxBITMAP_TYPE_PNG);
    if (!appImage.IsOk()) {
        wxMessageBox(wxT("无法打开文件: ") + imagePath);
    }
    appImage.Rescale(100, 100);
    wxBitmap appBitmap(appImage);
    wxIcon appIcon;
    appIcon.CopyFromBitmap(appBitmap);
    saveframe->SetIcon(appIcon);
    saveframe->Show(true);
}


